//
//  BSTabBar.m
//  Baisijie
//
//  Created by 王建伟 on 2017/3/2.
//  Copyright © 2017年 RangGuangyu. All rights reserved.
//

#import "BSTabBar.h"

@interface BSTabBar ()

/** 发布按钮 */
@property (nonatomic ,weak) UIButton *publishBtn;
/** 上一次点击的tabBarButton */
@property (nonatomic ,weak) UIControl *priviousClickTabBarButton;

@end

@implementation BSTabBar

- (UIButton *)publishBtn {
    if (!_publishBtn) {
        UIButton *publishBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [publishBtn setImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal];
        [publishBtn setImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateHighlighted];
        [publishBtn sizeToFit];
        [publishBtn addTarget:self action:@selector(publishBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:publishBtn];
        _publishBtn = publishBtn;
    }
    return _publishBtn;
}
- (void)publishBtnClick {
    if (self.publishBtnClickBlock) {
        self.publishBtnClickBlock();
    }
}
- (void)layoutSubviews {
    [super layoutSubviews];
    CGFloat buttonW = self.bs_width / (self.items.count + 1);
    CGFloat buttonX = 0;
    int i = 0;
    for (UIControl *tabBarButton in self.subviews) {
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            if (i == 0 && _priviousClickTabBarButton == nil) {
                _priviousClickTabBarButton = tabBarButton;
            }
            if (i == self.items.count / 2) {
                i += 1;
            }
            buttonX = i * buttonW;
            tabBarButton.frame = CGRectMake(buttonX, 1, buttonW, self.bs_height - 1);
            i ++;
            [tabBarButton addTarget:self action:@selector(tabBarButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        }
    }
    self.publishBtn.bs_centerX = self.bs_width * 0.5;
    self.publishBtn.bs_centerY = self.bs_height * 0.5;
}
- (void)tabBarButtonClick:(UIControl *)tabBarButton {
    if (_priviousClickTabBarButton == tabBarButton) {
        [BSNotificationCenter postNotificationName:BSTabBarButtonDidRepeatClickNotification object:nil];
    }
    _priviousClickTabBarButton = tabBarButton;
}

@end
